<template>
    <div class="app">
        <div>
        <div class="top-banner">
            <img :src="navbar">
        </div> 
        <div class="title-line">
          <span class="tit" style="font-size: 38px;">注册</span>
        </div>
        </div>
        <!-- 用户注册表单 -->
        <el-form ref="form" :model="form" label-width="80px" class="registerForm">
            <el-form-item  
            prop="username"
            :rules="[
                  { required: true, message: '请输入用户名', trigger: 'blur' },
                  { pattern: /\d|\w|\W/, message: '请输入正确的用户名', trigger: 'blur' }
                    ]"
            >
              <el-input v-model="form.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item 
            prop="password"
            :rules="[
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { pattern: /\d|\w/, message: '请输入正确的密码', trigger: 'blur' }
                    ]"
            >
              <el-input v-model="form.password" type="password" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item v-model="form.sex" >
            <el-select v-model="form.sex" placeholder="性别" style="float:left;width:145px">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
            </el-select>
            </el-form-item>
            <el-form-item
                prop="email"
                :rules="[
                  { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                  { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ]"
            >
            <el-input v-model="form.email" placeholder="邮箱"></el-input>
            </el-form-item>
            <el-form-item >
            <el-col :span="11">
            <el-form-item prop="birthday">
            <el-date-picker type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="selectTime" placeholder="请输入出生日期" v-model="form.birthday" style="float:left;width: 100%;"></el-date-picker>
            </el-form-item>
            </el-col>
            </el-form-item>
            <el-form-item  
            prop="address"
            :rules="[
                  { required: true, message: '请输入住址', trigger: 'blur' },
                  { pattern: /\d|\w|\W/, message: '请输入正确的住址', trigger: 'blur' }
                    ]"
            >
              <el-input v-model="form.address" placeholder="住址"></el-input>
            </el-form-item>
            <el-form-item   prop="checkCode"
                       :rules="[
                              { required: true, message: '请输入验证码', trigger: 'blur' },
                              { pattern: /\d|\w/, message: '请输入正确的验证码', trigger: 'blur' }
                       ]"
            >
                      <el-input v-model="form.checkCode" placeholder="请输入验证码">
                      </el-input>
                      <img :src="captcha" @click="getCaptcha" class="inline-block identifyCaptcha"/>
                      <a  href="#/user/login" style="text-decoration:none;float:right;"><span style="color:#00BFFF">返回登录></span></a>
            </el-form-item> 
            <el-form-item>
            <center>
             <el-button type="primary" @click="submitForm('form')" style="width:152px">注册</el-button>
             <el-button @click="resetForm('form')" style="width:152px;">重置</el-button>
            </center>
             </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {apiurl} from "../../service/api.js";
import background from '../../assets/navbar1.png'

export default {
    name : 'register',
    created() {
    this.getCaptcha();
    },
    data(){
        return{
            navbar : background,

             captchaform: {
                    CaptchaType: "character",
                    Id: '',
                    VerifyValue: '',
                    ConfigAudio: {
                        CaptchaLen: 6,
                        Language: 'zh'
                    },
                    ConfigCharacter: {
                        Height: 60,
                        Width: 240,
                        Mode: 2,
                        ComplexOfNoiseText: 0,
                        ComplexOfNoiseDot: 0,
                        IsUseSimpleFont: true,
                        IsShowHollowLine: false,
                        IsShowNoiseDot: false,
                        IsShowNoiseText: false,
                        IsShowSlimeLine: false,
                        IsShowSineLine: false,
                        CaptchaLen: 6,
                        BgColor:{R:255, G:102, B:0, A:255}
                    },
                    ConfigDigit: {
                        Height: 80,
                        Width: 240,
                        CaptchaLen: 5,
                        MaxSkew: 0.7,
                        DotCount: 80
                    }
                },

            form:{
                username: '',
                password: '',
                sex: '',
                email:'',
                birthday:'',
                address: '',
                checkCode:'',
            },
            captcha:'',
            identifyCaptcha: '',
        }

    },methods:{
        getCaptcha(){
            var that = this
            that.$axios.post({
              url: apiurl.getCaptcha,
              data:that.captchaform
            }).then(res => {
              if(res.data.code==200){  //获取验证码成功
                  that.captcha = res.data.data
                  that.captchaform.Id = res.data.captchaId
                  // that.captcha=res.data.data.checkCode
              }else{
                that.$message.error(`获取验证码失败`)
              }
            })
        },
        selectTime(val){
      this.form.birthday = val;
      // console.log(val);
        },
        submitForm(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {  //用户信息不能为空
              var that = this
              //构造请求信息
              var RegisterPostData = {
                //用户数据
                AccountName : that.form.username,
                Password : this.$md5(that.form.password),
                Sex : that.form.sex,
                Email :that.form.email,
                Birthday : that.form.birthday,
                Address : that.form.address,

                //验证码数据
                Id : that.captchaform.Id,
                VerifyValue : that.form.checkCode
              }
            that.$axios.post({
              url: apiurl.regist,
              data:RegisterPostData,
              type: 'application/json;charset=UTF-8'
            }).then(res => {
              if(res.data.code==200){  //注册成功
                  that.$message.success(`注册成功`)
                  that.resetForm('form')
              }else{
                that.$message.error(res.data.msg)
                that.getCaptcha()
              }
            })
            }else {
                return false
              }
          })
        },
        resetForm(formName) {
        this.$refs[formName].resetFields();
        },
    }
}
</script>

<style scpoed> /*css局部生效*/
    #app{
      position: relative;
      margin-top: 50px;
    }
    *{
    padding: 0;
    margin: 0;
    font-family: PingFangSC-Medium;
    -webkit-tap-highlight-color: rgba(255,255,255,0);
    }
    .bili-header-m {
    font: 12px 'Helvetica Neue', Helvetica, Arial, 'Microsoft Yahei', 'Hiragino Sans GB', 'Heiti SC', 'WenQuanYi Micro Hei', sans-serif;
    position: relative;
    z-index: 10000;
    background: #fff;
    margin: 0;
    padding: 0;
    }
    .top-banner {
    background: #00a0d8;
    height: 86px;
    text-align: center;
    margin-bottom: 20px;
    }
    a, img {
    border: none;
    }
    .title-line {
    width: 980px;
    height: 28px;
    margin: 0 auto;
    border-bottom: 1px solid #ddd;
    margin-bottom: 28px;
    text-align: center;
    }
    .title-line .tit {
    height: 56px;
    line-height: 56px;
    margin: 0 auto;
    padding: 0 20px;
    background: #fff;
    text-align: center;
    }
    .registerForm {
    width: 400px;
    margin: 0 auto;
    padding: 60px 0;
    }
    
    .identifyCaptcha {
    position: relative;
    top: 10px;
    width: 140px;
    height: 30px;
    border-radius: 2px;
    border: none;
    }

</style>